from rest_framework import serializers
from plumes.models import Channel, Article, Comment
from users.models import User


class ArticleSerializer(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields = ('id', 'title')


class FansSerializer(serializers.ModelSerializer):
    """粉丝序列化器"""
    class Meta:
        model = User
        fields = ('fans',)


class UserSerializer(serializers.ModelSerializer):
    """用户序列化器"""
    articles = ArticleSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


class ChannelListSerializer(serializers.ModelSerializer):
    """文章列表总序列化器"""
    user = UserSerializer()
    collected = serializers.SerializerMethodField(label='当前用户是否收藏')

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime', 'user',
                  'collected_users', 'collected', 'image', 'visits')

    def get_collected(self, obj):
        now_user = self.context['request'].user
        users = obj.collected_users.all()
        for user in users:
            if user == now_user:
                return True
        else:
            return False


class ChannelSerializer(serializers.ModelSerializer):
    """频道列表序列化器"""
    class Meta:
        model = Channel
        fields = ('id', 'name')


class CommentsSerializer(serializers.ModelSerializer):
    """评论序列化器"""
    user = UserSerializer()
    subs = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Comment
        fields = ('id', 'content', 'article', 'parent', 'createtime',
                  'updatetime', 'user', 'subs')

    def get_subs(self, instance):
        if instance.subs:
            return CommentsSerializer(instance.subs, many=True).data
        return


class ArticleInfoSerializer(serializers.ModelSerializer):
    """文章详情总序列化器"""
    user = UserSerializer()

    comments = serializers.SerializerMethodField()

    def get_comments(self, obj):
        article_comments = obj.comments.filter(parent_id=None)
        serializer = CommentsSerializer(article_comments, many=True)
        return serializer.data

    class Meta:
        model = Article
        fields = ('id', 'user', 'comments', 'title', 'content', 'image', 'createtime',
                  'updatetime', 'visits', 'thumbup', 'comment_count', 'channel', 'labels', 'collected_users')


class PublishArticleSerializer(serializers.ModelSerializer):
    """发布文章序列化器"""

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'channel', 'labels', 'image')

    def create(self, validated_data):
        user = self.context['request'].user
        validated_data['user'] = user
        return super().create(validated_data)


class PublishCommentSerializer(serializers.ModelSerializer):
    """评论文章序列化器"""
    class Meta:
        model = Comment
        fields = ('article', 'content', 'parent')

    def create(self, validated_data):
        user = self.context['request'].user
        validated_data['user'] = user
        return super().create(validated_data)


class ArticleSearchSerializer(serializers.ModelSerializer):
    """搜索文章序列化器"""
    text = serializers.SerializerMethodField()

    def get_text(self, obj):
        request = self.context['request']
        return request.query_params.get('text')

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime', 'text')


